package com.hopu.oa.service.impl.user;


import com.hopu.oa.dao.user.UserDao;
import com.hopu.oa.entity.dto.user.UserInformation;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserServiceImpl
 * @Description:
 * @Date: 2021/11/11 17:06
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    public List<AoaUser> selectAllUser(String loginTime, Integer userId) {
        return userDao.selectAllUser(loginTime, userId);
    }

    /*
            @Description : 根据 userName/Tel   和 password  查询用户
            @Date   :  2021/11/11
            @Param  : [userName, password]
            @return : com.hopu.oa.entity.user.AoaUser
             */
    @Override
    public AoaUser queryUserByNameOrTelAndPassword(String userName, String password) {
        return userDao.queryUserByNameOrTelAndPassword(userName, password);
    }


    /*
      @Description : 根据id查询用户
      @Date   :  2021/11/13
      @Param  : [userId]
      @return : com.hopu.oa.entity.user.AoaUser
     */
    @Override
    public AoaUser queryUserById(Integer userId) {
        return userDao.selectById(userId);
    }

    /*
    @Description :
                  * 校验用户 ： DTO VS POJO
                  * 校验用户是否修改密码
                  * 返回更新后的用户
    @Date   :  2021/11/12
    @Param  : [aoaUser, dtoUser]
    @return : java.util.Map<java.lang.String,java.lang.Object>
   */
    @Override
    public Map<String, Object> user_updateUserByUserId(AoaUser aoaUser, UserInformation dtoUser) {

        Map<String, Object> map = new HashMap<>();

        boolean flag = false;



     /*   if (aoaUser.getUserName().equals(dtoUser.getUserName()))
            aoaUser.setUserName(null);*/
        /*if(aoaUser.getUserName().equals(dtoUser.getUserName()))
            dtoUser.setUserName(null);*/


        if (aoaUser.getUserTel().equals(dtoUser.getUserTel())) {
            dtoUser.setUserTel(null);
        }

        if (aoaUser.getRealName().equals(dtoUser.getRealName())) {
            dtoUser.setRealName(null);
        }

        if (aoaUser.getEamil().equals(dtoUser.getEamil())) {
            dtoUser.setEamil(null);
        }

        if (aoaUser.getAddress().equals(dtoUser.getAddress())) {
            dtoUser.setAddress(null);
        }

        if (aoaUser.getUserEdu().equals(dtoUser.getUserEdu())) {
            dtoUser.setUserEdu(null);
        }

        if (aoaUser.getUserSchool().equals(dtoUser.getUserSchool())) {
            dtoUser.setUserSchool(null);
        }

        if (aoaUser.getUserIdcard().equals(dtoUser.getUserIdcard())) {
            dtoUser.setUserIdcard(null);
        }

        if (aoaUser.getBank().equals(dtoUser.getBank())) {
            dtoUser.setBank(null);
        }

        if (aoaUser.getSex().equals(dtoUser.getSex())) {
            dtoUser.setSex(null);
        }

        if (aoaUser.getThemeSkin().equals(dtoUser.getThemeSkin())) {
            dtoUser.setThemeSkin(null);
        }

        if (aoaUser.getBirth().equals(dtoUser.getBirth())) {
            dtoUser.setBirth(null);
        }


        //数据库部分标签是空，排除空指针异常
        if (aoaUser.getUserSign() != null) {
            if (aoaUser.getUserSign().equals(dtoUser.getUserSign())) {
                dtoUser.setUserSign(null);
            }
        }

        //用户可能没有填密码字段，排除空指针异常
        if (!(dtoUser.getPassword().equals(""))) {
            if (aoaUser.getPassword().equals(dtoUser.getPassword())) {
                dtoUser.setPassword(null);
            } else {
                flag = true;
            }
        }


        if (aoaUser.getImgPath().equals(dtoUser.getImgPath())) {
            dtoUser.setImgPath(null);
        }


        // POJO id 赋值给 DTO id
        dtoUser.setUserId(aoaUser.getUserId());

        //更新用户,并判断是否更改成功，装入map
        if (userDao.user_updateUserByUserId(dtoUser) > 0) {
            //重新查询用户
            map.put("user", userDao.selectById(aoaUser.getUserId()));
        }


        map.put("flag", flag);

        return map;


    }

    /*
     @Description : 根据用户id 查询部门名称 和  职位
     @Date   :  2021/11/13
    @Param  : [userId]
     @return : java.util.Map<java.lang.String,java.lang.String>
     */
    @Override
    public Map<String, String> queryUserDeptAndPositionById(Integer userId) {
        return userDao.queryUserDeptAndPositionById(userId);
    }

    @Override
    public List<AoaUser> deletePosition(Integer positionId) {
        List<AoaUser> aoaUsers = userDao.deletePosition(positionId);
        return aoaUsers;
    }

    @Override
    public List<AoaUser> selectByRole(Integer roleId) {
        return userDao.selectByRole(roleId);
    }

    @Override
    public AoaUser selectUserByUserName(String userName) {
        return userDao.selectUserByUserName(userName);
    }

}